﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Model.Module;
using Enterprise.Service.Module;
using Enterprise.Infrastructure;
using Enterprise.Control;
namespace Enterprise.UI.Web.Manage.Module
{
    public partial class Index : System.Web.UI.Page
    {
        protected int Id = (int)Utility.sink("Id", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        int mId = (int)Utility.sink("mId", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string Action = (string)Utility.sink("Action", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnCommand();
                OnStart();                
            }
        }

        private void OnStart()
        {
            ModuleService ser = new ModuleService();
            string url = "Create.aspx";
            if (Id != 0)
            {
                url += "?Id=" + Id;
            }
            CreateBT.AddButton(url, Utility.PopedomType.New, HeadMenuWebControls1);
            if (Id != 0)
            {
                string pUrl = "Index.aspx";
                var module = ser.ModuleDisp(Id);
                if (module != null)
                {
                    pUrl = "Index.aspx?Id=" + module.MPARENTID;
                }
                CreateBT.AddButton("back.gif", "返回", pUrl, Utility.PopedomType.List, HeadMenuWebControls1);
            }

            OnBindData();
        }

        private void OnBindData()
        {
            ModuleService service = new ModuleService();
            var q = service.ModuleList().Where(p => p.MPARENTID == Id).OrderBy(p => p.MORDERBY);
            GridView1.DataSource = q;
            GridView1.DataBind();
        }

        private void OnCommand()
        {
            if (!string.IsNullOrEmpty(Action) && mId != 0)
            {
                ModuleService mService = new ModuleService();
                switch (Action)
                {
                    case "Up":
                        mService.ModuleMoveUpService(mId);
                        break;
                    case "Down":
                        mService.ModuleMoveDownService(mId);
                        break;
                }
                Response.Redirect("Index.aspx?Id=" + Id);
                Response.End();
            }
        }

        protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onMouseOver"] = "js.ItemOver(this)";
            }
        }

        protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            ModuleService ser = new ModuleService();
            int index = Convert.ToInt32(e.CommandArgument); //获取行号
            int did = (int)GridView1.DataKeys[index].Value;
            var q = ser.ModuleDisp(did);
            if (q != null)
            {
                ModuleService service = new ModuleService();
                switch (e.CommandName)
                {
                    case "mClose":
                        q.MDELETE = "1";
                        q.DB_Option_Action = "UpdatemDelete";
                        service.ModuleExecute(q);
                        break;
                    case "mOpen":
                        q.MDELETE = "0";
                        q.DB_Option_Action = "UpdatemDelete";
                        service.ModuleExecute(q);
                        break;
                    case "mDelete":
                        q.DB_Option_Action = "Delete";
                        service.ModuleExecute(q);
                        break;
                }

                OnStart();
            }
        }


    }
}